package com.example.zhaohang.employmentapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.example.zhaohang.been.RegisterBeen;
import com.example.zhaohang.contect.UriContect;
import com.example.zhaohang.utils.GsonUtil;
import com.example.zhaohang.utils.ToastMake;
import com.example.zhaohang.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import okhttp3.Call;
import okhttp3.MediaType;

public class SaoMaActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private QRCodeView mQrCodeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sao_ma);
        mQrCodeView = (QRCodeView) findViewById(R.id.zxingview);
        mQrCodeView.setDelegate(this);

    }


    @Override
    public void onScanQRCodeSuccess(final String result) {

        mQrCodeView.showScanRect();


        //如果有网
        if (Utils.netConnected(SaoMaActivity.this)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int index = result.indexOf("=");
                    getData(result.substring(index + 1, result.length()));
                }
            }).start();

        }

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }
    @Override
    protected void onStart() {
        super.onStart();
        mQrCodeView.startCamera();
        //        mQRCodeView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);

        mQrCodeView.showScanRect();
    }

    @Override
    protected void onStop() {
        mQrCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQrCodeView.onDestroy();
        super.onDestroy();
    }
    private void getData(String mac) {

        Map<String, String> map = new HashMap<>();
        map.put("phone", MyApplayCation.phone);
        map.put("mac", mac);
        map.put("position", MyApplayCation.postion);
        String json = GsonUtil.parseMapToJson(map);

        OkHttpUtils
                .postString()
                .url(UriContect.INDEX)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast("扫码失败,请重新扫码");
                    }

                    @Override
                    public void onResponse(String response, int id) {

                        RegisterBeen been = GsonUtil.parseJsonToBean(response, RegisterBeen.class);

                        if (been==null){
                            ToastMake.showToast(getResources().getString(R.string.name18));
                            return;
                        }
                        if (!TextUtils.equals(been.getStatus(),"1")){
                            ToastMake.showToast(been.getMsg());
                            return;
                        }else {
                            finish();
                        }

//                        response
//                        if(response.getStatus().equals("0")){
//                            Toast.makeText(getApplicationContext(),response.getMsg(),Toast.LENGTH_SHORT).show();
//                        }else if(response.getStatus().equals("1")){
//                            Toast.makeText(getApplicationContext(),response.getMsg(),Toast.LENGTH_SHORT).show();
//                            Bundle bundle = new Bundle();
//                            bundle.putString("position",response.getPosition());
//                            Intent intent = new Intent();
//                            intent.putExtras(bundle);
//                            // 返回intent
//                            setResult(SanCodeActivity.RESULT_OK, intent);
//                            finish();
//                        }
                    }
                });

    }


}
